package com.dean.test.ui;

import com.dean.ui.panel.impl.DbQueryPanel2;
import com.formdev.flatlaf.FlatIntelliJLaf;
import com.formdev.flatlaf.extras.components.FlatSplitPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.util.Enumeration;


/**
 * @author dean
 * @version 1.0
 * @description: TODO
 * @date 2024/8/2 11:15
 */
public class DeanToolMainNewUI {
    private final Logger logger = LoggerFactory.getLogger(DeanToolMainNewUI.class);
    private JPanel root;
    private JTree menuTree;
    private FlatSplitPane splitPane;
    private JTabbedPane menuTab;
    {
        logger.info("dean tool start ......");
        JFrame frame = new JFrame("DeanTool小工具");
        // 开启UI美化
        try {
            // 设置主题
            UIManager.setLookAndFeel(new FlatIntelliJLaf());
            // UIManager.setLookAndFeel(new FlatDarculaLaf());
            // UIManager.setLookAndFeel(new BeautyEyeLookAndFeelCross());
            // BeautyEyeLNFHelper.launchBeautyEyeLNF();
            // 给所有页面都使用该主题
            SwingUtilities.updateComponentTreeUI(frame);

            // 最合适的方式显示
//            frame.pack();

        } catch (Exception e1) {
            logger.error("美化工具初始化失败:{}",e1.getMessage(),e1);
        }
        initGlobalFontSetting(new Font("微软雅黑",Font.PLAIN,12));
        setupUI();
        frame.setContentPane(root);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        // 获取屏幕大小
        logger.debug("屏幕尺寸:{}",toolkit.getScreenSize());
        int width = new Double(toolkit.getScreenSize().width*0.85).intValue();
        int height = new Double(toolkit.getScreenSize().height*0.8).intValue();
        frame.setMinimumSize(new Dimension(width,height));
        frame.pack();
        frame.setVisible(true);
        // FlatSplitPane  必须在窗体显示之后才起效
        splitPane.setDividerLocation(0.1);
        frame.setLocationRelativeTo(null);
    }

    private JTree menuTree() {
        DefaultMutableTreeNode rootMenu = new DefaultMutableTreeNode("菜单列表");
        DefaultMutableTreeNode sysMenu = new DefaultMutableTreeNode("系统管理");
        DefaultMutableTreeNode dataMenu = new DefaultMutableTreeNode("数据开发");
        DefaultMutableTreeNode testMenu = new DefaultMutableTreeNode("测试功能");
        for(int i=0;i<100;i++){
            testMenu.add(new DefaultMutableTreeNode("测试菜单"+i));
        }
        sysMenu.add(new DefaultMutableTreeNode("用户管理"));
        sysMenu.add(new DefaultMutableTreeNode("部门管理"));
        rootMenu.add(sysMenu);
        rootMenu.add(dataMenu);
        rootMenu.add(testMenu);
        menuTree = new JTree(rootMenu);
        return menuTree;
    }


    private void setupUI(){
        root = new JPanel();
        // 自行布局
        root.setLayout(new BorderLayout());
        // 将屏幕拆分为左右两块
        splitPane = new FlatSplitPane();
        // 一键展开
        //splitPane.setOneTouchExpandable(true);
        // 左边菜单栏
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setVisible(true);
        scrollPane.setViewportView(menuTree());
        splitPane.setLeftComponent(scrollPane);
        // 右边内容展示区域

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("数据查询",new DbQueryPanel2().showScrollView());
        splitPane.setRightComponent(tabbedPane);
        root.add(splitPane);

    }

    /**
     * 设置全局字体
     * @param fnt
     */
    private void initGlobalFontSetting(Font fnt){
        FontUIResource fontRes = new FontUIResource(fnt);
        for(Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();){
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if(value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }

    public static void main(String[] args) {
        new DeanToolMainNewUI();
    }
}
